package ihm;

import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import metier.Circuit;
import dao.DaoCircuit;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Color;


public class ConsulterCircuit extends JFrame implements ActionListener, MouseListener 
{
	private JLabel lblListeDesCircuits;
	private JLabel lblInfoCircuit;
	private static JTable table;
	private static JScrollPane tabletest;
	private static DefaultTableModel dtm;
	private JLabel lblCircuitNumro;
	private JLabel lblNumC;
	private JLabel lbldesC;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) 
	{
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConsulterCircuit frame = new ConsulterCircuit();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ConsulterCircuit() 
	{
		
		setTitle("Consulter circuit");
		setBounds(100, 100, 800, 600);
		getContentPane().setLayout(null);
		
		lblListeDesCircuits = new JLabel("Liste des circuits :");
		lblListeDesCircuits.setBounds(10, 40, 90, 14);
		getContentPane().add(lblListeDesCircuits);
		
		lblInfoCircuit = new JLabel("Information sur le circuit");
		lblInfoCircuit.setForeground(Color.RED);
		lblInfoCircuit.addMouseListener(this);
		lblInfoCircuit.setBounds(61, 424, 122, 14);
		getContentPane().add(lblInfoCircuit);
		
		tabletest = new JScrollPane();
		tabletest.addMouseListener(this);
		tabletest.setBounds(10, 65, 212, 348);
		getContentPane().add(tabletest);
		
		ConsulterCircuit.table = new JTable();
		tabletest.setViewportView(table);
		
		lblCircuitNumro = new JLabel("Circuit num\u00E9ro :");
		lblCircuitNumro.setBounds(462, 65, 81, 14);
		getContentPane().add(lblCircuitNumro);
		
		lblNumC = new JLabel("");
		lblNumC.setBounds(484, 90, 46, 14);
		getContentPane().add(lblNumC);
		
		lbldesC = new JLabel("");
		lbldesC.setBounds(354, 144, 338, 269);
		getContentPane().add(lbldesC);
		updateTable();
		ajoutCircuitTableau();
	}
		
	
	public static void updateTable() 
	{
			
		String[] titre = {
						"Circuit",
						"Dur\u00E9e",
	                	"Info +"};
			
		Object[][] listeCircuit = new Object[0][3];
		dtm = new DefaultTableModel(listeCircuit, titre)
		{
		public boolean isCellEditable(int row, int col) 
		{
		boolean res = false;
		if (col == 3)
			res = true;
		return res;
		};
		};
		table.setModel(dtm);
	}
		
		
		
	public static void ajoutCircuitTableau()
	{
			
		Vector <Circuit> lescircuits = DaoCircuit.getLesCircuits();
		for(Circuit unCircuit : lescircuits)
		{
			/* ajout d'une nouvelle ligne au tableau des circuits */
			dtm.addRow(new Object[]{unCircuit.getIdCircuit(),unCircuit.getDureeCircuit()}); 
		}
		table.setModel(dtm);
	}

		
	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
			// TODO Auto-generated method stub
			
	}
	public void mouseClicked(MouseEvent e) 
	{
		int lignetable = table.getSelectedRow();
		lblNumC.setText(String.valueOf(table.getValueAt(lignetable, 0)));
		lbldesC.setText(DaoCircuit.getLeCircuit(table.getValueAt(lignetable, 0).hashCode()).getDescriptionCircuit());
	}
	public void mouseEntered(MouseEvent e) {
		lblInfoCircuit.setCursor(new Cursor(Cursor.HAND_CURSOR));
	}
	public void mouseExited(MouseEvent e) {
		
	}
	public void mousePressed(MouseEvent e) {
		
	}
	public void mouseReleased(MouseEvent e) {
		
		
	}
	
}
